<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>Linux系列一之常用命令 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="简介　　Groovy是用于Java虚拟机的一种敏捷的动态语言，它是一种成熟的面向对象编程语言，既可以用于面向对象编程，又可以用作纯粹的脚本语言。使用该种语言不必编写过多的代码，同时又具有闭包和动态语言中的其他特性。该语言特别适合与Spring的动态语言支持一起使用，设计时充分考虑了Java集成，这使 Groovy 与 Java 代码的互操作很容易。
开发环境　　搭建groovy的开发环境，无需单独">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux系列一之常用命令">
<meta property="og:url" content="http://yoursite.com/2015/12/01/groovy/1.groovy入门/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="简介　　Groovy是用于Java虚拟机的一种敏捷的动态语言，它是一种成熟的面向对象编程语言，既可以用于面向对象编程，又可以用作纯粹的脚本语言。使用该种语言不必编写过多的代码，同时又具有闭包和动态语言中的其他特性。该语言特别适合与Spring的动态语言支持一起使用，设计时充分考虑了Java集成，这使 Groovy 与 Java 代码的互操作很容易。
开发环境　　搭建groovy的开发环境，无需单独">
<meta property="og:image" content="http://yoursite.com/images/groov-start.png">
<meta property="og:updated_time" content="2016-01-29T17:47:36.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Linux系列一之常用命令">
<meta name="twitter:description" content="简介　　Groovy是用于Java虚拟机的一种敏捷的动态语言，它是一种成熟的面向对象编程语言，既可以用于面向对象编程，又可以用作纯粹的脚本语言。使用该种语言不必编写过多的代码，同时又具有闭包和动态语言中的其他特性。该语言特别适合与Spring的动态语言支持一起使用，设计时充分考虑了Java集成，这使 Groovy 与 Java 代码的互操作很容易。
开发环境　　搭建groovy的开发环境，无需单独">
  
    <link rel="alternative" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="/css/style.css" type="text/css">
  

</head>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-groovy/1.groovy入门" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2015/12/01/groovy/1.groovy入门/" class="article-date">
  <time datetime="2015-11-30T17:00:00.000Z" itemprop="datePublished">2015-12-01</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/groovy/">groovy</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Linux系列一之常用命令
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="u7B80_u4ECB"><a href="#u7B80_u4ECB" class="headerlink" title="简介"></a>简介</h2><p>　　Groovy是用于Java虚拟机的一种敏捷的动态语言，它是一种成熟的面向对象编程语言，既可以用于面向对象编程，又可以用作纯粹的脚本语言。使用该种语言不必编写过多的代码，同时又具有闭包和动态语言中的其他特性。该语言特别适合与Spring的动态语言支持一起使用，设计时充分考虑了Java集成，这使 Groovy 与 Java 代码的互操作很容易。</p>
<h2 id="u5F00_u53D1_u73AF_u5883"><a href="#u5F00_u53D1_u73AF_u5883" class="headerlink" title="开发环境"></a>开发环境</h2><p>　　搭建groovy的开发环境，无需单独安装groovy，直接安装插件即可！新建一个Java Project，加入Groovy相关的包：右键项目 -&gt; Configure -&gt; Convert to Groovy Project。在项目groovy源文件下右键，New -&gt; Other -&gt; Groovy -&gt; Groovy Class。自动生成的源代码，加入main方法。groovy中默认的修饰符就是public，main方法可以去掉public。完整代码如下所示：<br><img src="/images/groov-start.png" alt="groov-start.png"><br>　　不采用修饰符public,默认存在getTitle方法，如果采用了修饰符public，getTitle方法不存在,但可以直接访问title属性<br>　　一个变量的类型在运行中随时可以改变，一切根据需要而定。如果你赋给它boolean，它接收boolean值之后就会自动把类型转变为boolean值。看下面的代码：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">def var=<span class="string">"hello "</span>+</span><br><span class="line">       <span class="string">"world"</span>+</span><br><span class="line">       <span class="string">",groovy!"</span></span><br><span class="line">       println var;</span><br><span class="line">       println var.class;</span><br><span class="line">       var=<span class="number">1001</span></span><br><span class="line">       println var.class</span><br><span class="line"><span class="comment">/*</span><br><span class="line">输出结果：</span><br><span class="line">hello world,groovy!</span><br><span class="line">class java.lang.String</span><br><span class="line">class java.lang.Integer</span><br><span class="line">*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//循环</span></span><br><span class="line">def var=<span class="string">"hello "</span>+</span><br><span class="line">    <span class="string">"world"</span>+</span><br><span class="line">    <span class="string">",groovy!"</span></span><br><span class="line"><span class="function">def <span class="title">repeat</span><span class="params">(val)</span></span>&#123;</span><br><span class="line">	<span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++)&#123;</span><br><span class="line">	 println val</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">repeat(var)</span><br><span class="line"><span class="comment">//输出：</span></span><br><span class="line">hello world,groovy!</span><br><span class="line">hello world,groovy!</span><br><span class="line">hello world,groovy!</span><br><span class="line">hello world,groovy!</span><br><span class="line">hello world,groovy!</span><br></pre></td></tr></table></figure></p>
<h2 id="Gstring"><a href="#Gstring" class="headerlink" title="Gstring"></a>Gstring</h2><p>　　除了标准的java.lang.String以外（用单引号括住），groovy还支持Gstring字符串类型（用双引号括住）。把上面的for循环中的语句改成：println “This is ${i}:${val}”/<em>避免了拼接字符窜</em>/<br>输出：<br>This is 0:hello world,groovy!<br>This is 1:hello world,groovy!<br>This is 2:hello world,groovy!<br>This is 3:hello world,groovy!<br>This is 4:hello world,groovy!</p>
<h2 id="u8303_u56F4"><a href="#u8303_u56F4" class="headerlink" title="范围"></a>范围</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//输出 1 2 3 4 5</span></span><br><span class="line"><span class="keyword">for</span>(i in <span class="number">1</span>..<span class="number">5</span>)&#123;</span><br><span class="line">    println <span class="string">"$&#123;i&#125;"</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//输出 1 2 3 4</span></span><br><span class="line"><span class="keyword">for</span>(i in <span class="number">1</span>..&lt;<span class="number">5</span>)&#123;</span><br><span class="line">    println <span class="string">"$&#123;i&#125;"</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//语法错误</span></span><br><span class="line"><span class="keyword">for</span>(i in <span class="number">1</span>..&lt;=<span class="number">5</span>)&#123;</span><br><span class="line">    println <span class="string">"$&#123;i&#125;"</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//给方法指定默认参数值</span></span><br><span class="line"><span class="function">def <span class="title">repeat</span><span class="params">(val, repeat=<span class="number">3</span>)</span></span>&#123;</span><br><span class="line">    <span class="keyword">for</span>(i in <span class="number">0</span>..&lt;repeat)&#123;</span><br><span class="line">        println <span class="string">"This is $&#123;i&#125;:$&#123;val&#125;"</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span><br><span class="line"> * 输出</span><br><span class="line"> * This is 0:shc</span><br><span class="line"> * This is 1:shc</span><br><span class="line"> * This is 2:shc</span><br><span class="line"> * This is 3:shc</span><br><span class="line"> * This is 4:shc</span><br><span class="line"> */</span></span><br><span class="line">repeat(<span class="string">'shc'</span>, <span class="number">5</span>)</span><br><span class="line"><span class="comment">/**</span><br><span class="line"> * 输出</span><br><span class="line"> * This is 0:shc</span><br><span class="line"> * This is 1:shc</span><br><span class="line"> * This is 2:shc</span><br><span class="line"> */</span></span><br><span class="line">repeat(<span class="string">'shc'</span>)<span class="comment">/*未指定第二个参数*/</span></span><br></pre></td></tr></table></figure>
<h2 id="u96C6_u5408"><a href="#u96C6_u5408" class="headerlink" title="集合"></a>集合</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">def collect=[<span class="string">"a"</span>,<span class="string">"b"</span>,<span class="string">"c"</span>]</span><br><span class="line">collect.add(<span class="number">1</span>);</span><br><span class="line">collect&lt;&lt;<span class="string">"come on"</span>;</span><br><span class="line">collect[collect.size()]=<span class="number">100.0</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span><br><span class="line"> * 输出[a, b, c, 1, come on, 100.0]</span><br><span class="line"> */</span></span><br><span class="line">println collect</span><br><span class="line"></span><br><span class="line"><span class="comment">/*Collection使用类似数组下标的方式进行检索：*/</span></span><br><span class="line">println collect[collect.size()-<span class="number">1</span>]</span><br><span class="line">println collect[<span class="number">5</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">/*groovy支持负索引：*/</span></span><br><span class="line">println collect[-<span class="number">1</span>]<span class="comment">//最后一个元素从-1开始，输出100.0</span></span><br><span class="line">println collect[-<span class="number">2</span>] <span class="comment">//索引其倒数第2个元素</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*Collection支持集合运算：*/</span></span><br><span class="line">collect=collect+<span class="number">5</span> <span class="comment">//在集合中添加元素5</span></span><br><span class="line">println collect[collect.size()-<span class="number">1</span>]</span><br><span class="line"></span><br><span class="line">collect=collect-<span class="string">'a'</span> <span class="comment">//在集合中减去元素a(第1个)</span></span><br><span class="line">println collect[<span class="number">0</span>] <span class="comment">//现在第1个元素变成b了</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*往集合中添加另一个集合或删除一个集合：*/</span></span><br><span class="line">collect=collect-collect[<span class="number">0</span>..<span class="number">4</span>] <span class="comment">//把集合中的前5个元素去掉</span></span><br><span class="line">println collect[<span class="number">0</span>] <span class="comment">//现在集合中仅有一个元素，即原来的最后一个元素</span></span><br><span class="line">println collect[-<span class="number">1</span>] <span class="comment">//也可以用负索引，证明最后一个元素就是第一个元素</span></span><br></pre></td></tr></table></figure>
<h2 id="Map"><a href="#Map" class="headerlink" title="Map"></a>Map</h2><p>　　Map是“键-值”对的集合，在groovy中，键不一定是String，可以是任何对象(实际上Groovy中的Map就是java.util.Linke dHashMap)。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//定义一个Map: </span></span><br><span class="line">def map=[<span class="string">'name'</span>:<span class="string">'john'</span>,<span class="string">'age'</span>:<span class="number">14</span>,<span class="string">'sex'</span>:<span class="string">'boy'</span>]</span><br><span class="line"><span class="comment">//添加项：</span></span><br><span class="line">map=map+[<span class="string">'weight'</span>:<span class="number">25</span>]       <span class="comment">//添加john的体重</span></span><br><span class="line">map.put(<span class="string">'length'</span>,<span class="number">1.27</span>)      <span class="comment">//添加john的身高</span></span><br><span class="line">map.father=<span class="string">'Keller'</span>         <span class="comment">//添加john的父亲</span></span><br><span class="line"><span class="comment">//可以用两种方式检索值：</span></span><br><span class="line">println map[<span class="string">'father'</span>]       <span class="comment">//通过key作为下标索引 输出Keller</span></span><br><span class="line">println map.length          <span class="comment">//通过key作为成员名索引 输出1.27</span></span><br></pre></td></tr></table></figure></p>
<h2 id="u95ED_u5305"><a href="#u95ED_u5305" class="headerlink" title="闭包"></a>闭包</h2><p>　　闭包是用{}符号括起来的代码块，可以被单独运行或调用，也可以被命名。类似‘匿名类’或内联函数的概念。闭包中最常见的应用是对集合进行迭代，为了传递参数给闭包，闭包有一组可选的参数列表，通过“-&gt;”表示列表的结束，示例如下：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//闭包,输出1 2 3 ， entry为参数列表</span></span><br><span class="line">[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>].each&#123;</span><br><span class="line"> entry -&gt; print entry + <span class="string">' '</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//等价于</span></span><br><span class="line">[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>].each (&#123;</span><br><span class="line"> entry -&gt; print entry + <span class="string">' '</span></span><br><span class="line">&#125;)下面定义了<span class="number">3</span>个闭包对map进行了迭代：</span><br><span class="line"></span><br><span class="line"><span class="comment">//key,value两个参数用于接受每个元素的键/值</span></span><br><span class="line"><span class="comment">//以下两种写法等效</span></span><br><span class="line">map.each(&#123;key,value-&gt;println <span class="string">"$key:$value"</span>&#125;)<span class="comment">//闭包作为一个参数传递给map的each方法</span></span><br><span class="line">map.each (&#123;key, value -&gt; println <span class="string">"$&#123;key&#125;,:$&#123;value&#125;"</span>&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">//it是一个关键字，代表map集合的每个元素</span></span><br><span class="line">map.each&#123;println it&#125;     </span><br><span class="line">map.each(&#123; println it.getKey()+<span class="string">"--&gt;"</span>+it.getValue()&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">//除了用于迭代之外，闭包也可以单独定义：</span></span><br><span class="line">def say=&#123;word -&gt; println <span class="string">"Hi,$word!"</span>&#125;</span><br><span class="line">say(<span class="string">'groovy'</span>) <span class="comment">//输出：Hi,groovy!</span></span><br><span class="line">say.call(<span class="string">"groovy2"</span>) <span class="comment">//输出Hi,groovy2!</span></span><br><span class="line"></span><br><span class="line"><span class="function">def <span class="title">benchmark</span><span class="params">(Integer repeat, Closure worker)</span></span>&#123;</span><br><span class="line">    start = System.currentTimeMillis()</span><br><span class="line">    <span class="comment">//注意闭包的调用方法</span></span><br><span class="line">    repeat.times &#123; item -&gt; worker(item)&#125;   <span class="comment">//repeat.times &#123; item -&gt; worker.call(item)&#125;</span></span><br><span class="line">    stop = System.currentTimeMillis()</span><br><span class="line">    <span class="keyword">return</span> stop - start</span><br><span class="line">&#125;</span><br><span class="line">def slow = benchmark(<span class="number">10000</span>)&#123; tmp -&gt;</span><br><span class="line">    (<span class="keyword">int</span>) tmp / <span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line">def fast = benchmark(<span class="number">10000</span>)&#123;</span><br><span class="line">    it.intdiv(<span class="number">2</span>)</span><br><span class="line">&#125;</span><br><span class="line">println slow + <span class="string">' '</span> + <span class="function">fast</span><br><span class="line"></span><br><span class="line">def <span class="title">benchmark</span><span class="params">(Integer repeat, Closure worker)</span></span>&#123;</span><br><span class="line"> start = System.currentTimeMillis()</span><br><span class="line"> repeat.times &#123;worker(it)&#125; <span class="comment">//times闭包默认参数为it，其值从0到9999</span></span><br><span class="line"> stop = System.currentTimeMillis()</span><br><span class="line"> <span class="keyword">return</span> stop - start</span><br><span class="line">&#125;</span><br><span class="line">def slow = benchmark(<span class="number">10000</span>)&#123;</span><br><span class="line"> (<span class="keyword">int</span>) it / <span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line">def fast = benchmark(<span class="number">10000</span>)&#123;</span><br><span class="line"> it.intdiv(<span class="number">2</span>)</span><br><span class="line">&#125;</span><br><span class="line">println slow + <span class="string">' '</span> + fast</span><br><span class="line"></span><br><span class="line"><span class="comment">//查看传入闭包的参数</span></span><br><span class="line"><span class="function">def <span class="title">aa</span><span class="params">(Closure closure)</span></span>&#123;</span><br><span class="line"> closure.getParameterTypes().size()</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//闭包默认存在一个参数，输出1</span></span><br><span class="line">println aa&#123;&#125;</span><br><span class="line"><span class="comment">//向闭包传入一个参数，输出1 </span></span><br><span class="line">println aa&#123;one -&gt;&#125;</span><br><span class="line"><span class="comment">//向闭包传入两个参数，输出2</span></span><br><span class="line">println aa&#123;one, two -&gt;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//Closure 的 curry 方法返回当前闭包的一个克隆品，这个克隆品已经绑定 了一个或者多个给定的参数，参数的绑定是从左向右进行的</span></span><br><span class="line">def adder = &#123;x, y -&gt;</span><br><span class="line"> println x + <span class="string">' '</span> + y<span class="comment">//输出1 5</span></span><br><span class="line"> x + y&#125;</span><br><span class="line">def addOne = adder.curry(<span class="number">1</span>) <span class="comment">//先传入参数1</span></span><br><span class="line"><span class="function">println <span class="title">addOne</span><span class="params">(<span class="number">5</span>)</span></span><br><span class="line"></span><br><span class="line">adder </span>= &#123;x, y, z -&gt;</span><br><span class="line"> println x + <span class="string">' '</span> + y + <span class="string">' '</span> + z<span class="comment">//输出1 2 3</span></span><br><span class="line"> x + y + z&#125;</span><br><span class="line">addOne = adder.curry(<span class="number">1</span>, <span class="number">2</span>) <span class="comment">//先传入参数1 2</span></span><br><span class="line"><span class="function">println <span class="title">addOne</span><span class="params">(<span class="number">3</span>)</span><span class="comment">//输出6</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span><br><span class="line">假设你需要实现一个日志记录器，它应该支持行数的过滤，</span><br><span class="line">日志的格式化，并且输出它们到一个设备上，</span><br><span class="line">每一个记录器都应该是可配置的，办法是提供一个闭包作为每一个记录器自定义版本，</span><br><span class="line">这仍旧允许你实现应用一个过滤器，格式化和最终的日志输出，</span><br><span class="line">下列的代码显示了 curry 怎样被用来注入到自定义的记录器中：</span><br><span class="line">*/</span></span><br><span class="line"><span class="comment">//定义一个闭包，闭包的参数仍是闭包</span></span><br><span class="line">def configurator </span>= &#123;format, filter, line -&gt;</span><br><span class="line"> filter(line) ? format(line) : <span class="keyword">null</span></span><br><span class="line">&#125;</span><br><span class="line">def appender = &#123;config, append, line -&gt;</span><br><span class="line"> def out = config(line)</span><br><span class="line"> <span class="keyword">if</span>(out) append(out)</span><br><span class="line">&#125;</span><br><span class="line">def dateFormatter = &#123;line -&gt; <span class="string">'$&#123;new Date()&#125;: $&#123;line&#125;'</span>&#125;</span><br><span class="line">def debugFilter = &#123;line -&gt; line.contains(<span class="string">'debug'</span>)&#125;</span><br><span class="line">def consoleAppender = &#123;line -&gt; println line&#125;</span><br><span class="line">def myConf = configurator.curry(dateFormatter, debugFilter)</span><br><span class="line">def myLog = appender.curry(myConf, consoleAppender)</span><br><span class="line">myLog(<span class="string">'here is some debug message'</span>)</span><br><span class="line">myLog(<span class="string">'this will not be printed'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span><br><span class="line">我们想写一个生成积聚的函数，这个函数接收一个数字n，并 且返回一个函数，这个函数接收另外一个参数i ，并且返回n+i 的结果*/</span></span><br><span class="line"><span class="comment">//使用闭包实现</span></span><br><span class="line"><span class="function">def <span class="title">foo</span><span class="params">(n)</span></span>&#123;</span><br><span class="line"> <span class="keyword">return</span> &#123;n += it&#125;</span><br><span class="line">&#125;</span><br><span class="line">def accumulator = foo(<span class="number">1</span>)</span><br><span class="line"><span class="function">println <span class="title">accumulator</span><span class="params">(<span class="number">2</span>)</span></span><br><span class="line">println <span class="title">accumulator</span><span class="params">(<span class="number">2</span>)</span></span><br><span class="line"><span class="comment">//等价于</span></span><br><span class="line">n </span>= <span class="number">1</span></span><br><span class="line">def accumulator2 = &#123;</span><br><span class="line"> n += it&#125;</span><br><span class="line"><span class="function">println <span class="title">accumulator2</span><span class="params">(<span class="number">2</span>)</span></span><br><span class="line">println <span class="title">accumulator2</span><span class="params">(<span class="number">2</span>)</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//闭包</span></span><br><span class="line">class Mother</span>&#123;</span><br><span class="line"> <span class="keyword">int</span> field = <span class="number">1</span></span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">foo</span><span class="params">()</span></span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="number">2</span></span><br><span class="line"> &#125;</span><br><span class="line"> <span class="function">Closure <span class="title">birth</span><span class="params">(param)</span></span>&#123;</span><br><span class="line">  def local = <span class="number">3</span></span><br><span class="line">  def closure = &#123;caller -&gt;</span><br><span class="line">   [<span class="keyword">this</span>, field, foo(), local, param, caller, owner]</span><br><span class="line">   </span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> closure</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br><span class="line">Mother julia = <span class="keyword">new</span> Mother();</span><br><span class="line">closure = julia.birth(<span class="number">4</span>)</span><br><span class="line">println <span class="keyword">this</span></span><br><span class="line">context = closure.call(<span class="keyword">this</span>)</span><br><span class="line"><span class="comment">//输出</span></span><br><span class="line"><span class="comment">//Class1@757fe679</span></span><br><span class="line"><span class="comment">//Mother</span></span><br><span class="line"><span class="comment">//[Mother@2ad631, 1, 2, 3, 4, Class1@757fe679, Mother@2ad631]</span></span><br><span class="line"></span><br><span class="line">println context[<span class="number">0</span>].class.name</span><br><span class="line">println context</span><br><span class="line"><span class="keyword">assert</span> context[<span class="number">5</span>] <span class="keyword">instanceof</span> Script</span><br><span class="line"><span class="keyword">assert</span> context[<span class="number">6</span>] <span class="keyword">instanceof</span> Mother</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Counter</span></span>&#123;</span><br><span class="line"> <span class="keyword">public</span> count = <span class="number">0</span></span><br><span class="line"> </span><br><span class="line"> <span class="function">Object <span class="title">get</span><span class="params">(String name)</span></span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="string">'pretend value'</span></span><br><span class="line"> &#125;</span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">set</span><span class="params">(String name, Object value)</span></span>&#123;</span><br><span class="line"> </span><br><span class="line">  count ++</span><br><span class="line">  <span class="comment">//name = value;</span></span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br><span class="line">def pretender = <span class="keyword">new</span> Counter()</span><br><span class="line"><span class="keyword">assert</span> pretender.isNoField == <span class="string">'pretend value'</span></span><br><span class="line"><span class="keyword">assert</span> pretender.count == <span class="number">0</span></span><br><span class="line">pretender.isNoFieldEither = <span class="string">'just to increase counter'</span></span><br><span class="line"><span class="keyword">assert</span> pretender.count == <span class="number">1</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//输出5 1 -- 6 2 -- 8 3 -- 11</span></span><br><span class="line">def result = (<span class="number">1</span>..<span class="number">3</span>).inject(<span class="number">5</span>) &#123;</span><br><span class="line"> sum, i -&gt;</span><br><span class="line"> print <span class="string">"$sum $i -- "</span></span><br><span class="line"> sum += i</span><br><span class="line">&#125;</span><br><span class="line">println result</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MrBean</span></span>&#123;</span><br><span class="line"> String firstName, <span class="function">lastName</span><br><span class="line"> </span><br><span class="line"> String <span class="title">getName</span><span class="params">()</span></span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="string">"$firstName $lastName"</span></span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br><span class="line">def bean = <span class="keyword">new</span> MrBean(firstName: <span class="string">'Rowan'</span>)</span><br><span class="line">bean.lastName = <span class="string">'Atkinson'</span><span class="comment">//调用setter</span></span><br><span class="line">print bean.name<span class="comment">//调用getter</span></span><br><span class="line"></span><br><span class="line">规则：当属性和相应的访问方法对调用者都是可用的时候，属性引用被解析为对访问方</span><br><span class="line">法的调用，如果只存在一种可用，这是可选的</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Pro</span></span>&#123;</span><br><span class="line"> def someProperty</span><br><span class="line"> <span class="keyword">public</span> publicField</span><br><span class="line"> <span class="keyword">private</span> privateField</span><br><span class="line">&#125;</span><br><span class="line">def obj = <span class="keyword">new</span> Pro()</span><br><span class="line">def store = []</span><br><span class="line">obj.properties.each &#123; property -&gt;</span><br><span class="line"> store += property.key</span><br><span class="line"> store += property.value</span><br><span class="line">&#125;</span><br><span class="line">println store.contains(<span class="string">'someProperty'</span>)<span class="comment">//true</span></span><br><span class="line">println store.contains(<span class="string">'publicField'</span>)<span class="comment">//false</span></span><br><span class="line">println store.contains(<span class="string">'privateField'</span>)<span class="comment">//false</span></span><br><span class="line">println store<span class="comment">//[class, class Pro, someProperty, null]</span></span><br><span class="line">println obj.properties.size()<span class="comment">//2</span></span><br></pre></td></tr></table></figure></p>
<p>　　看起来，闭包类似于方法，需要定义参数和要执行的语句，它也可以通过名称被调用。然而闭包对象（不要奇怪，闭包也是对象）可以作为参数传递（比如前面的闭包作为参数传递给了map的each方法）。而在java中，要做到这一点并不容易（也许C++中的函数指针可以，但不要忘记java中没有指针）。其次，闭包也可以不命名（当然作为代价，只能在定义闭包时执行一次），而方法不可以。</p>
<h2 id="u7C7B"><a href="#u7C7B" class="headerlink" title="类"></a>类</h2><p>　　Groovy类和java类一样，可以用标准java bean的语法定义一个groovy类，也可以使用更groovy的方式定义和使用类，这样可以少写一半以上的javabean代码：</p>
<ol>
<li>不需要public修饰符，如前面所言，groovy的默认访问修饰符就是public，如果你的groovy类成员需要public修饰，则你根本不用写它。</li>
<li>不需要类型说明，同样前面也说过，groovy也不关心变量和方法参数的具体类型。</li>
<li>不需要getter/setter方法，在很多ide（如eclipse）早就可以为序员自动产生getter/setter方法了。在groovy中，则彻底不需要getter/setter方法——所有类成员（如果是默认的public）根本不用通过getter/setter方法引用它们（当然，如果你一定要通过get/set方法访问成员属性，groovy也提供了它们）。</li>
<li>不需要构造函数，不在需要程序员声明任何构造函数，因为groovy自动提供了足够你使用的构造函数。不用担心构造函数不够多，因为实际上只需要两个构造函数（1个不带参数的默认构造函数，1个只带一个map参数的构造函数—由于是map类型，通过这个参数你可以在构造对象时任意初始化它的成员变量）。</li>
<li>不需要return，Groovy 默认返回方法的最后一行的值。Groovy中，方法不需要return来返回值吗？这个似乎很难理解。看后面的代码吧。</li>
<li>不需要()号，Groovy中方法调用可以省略()号（构造函数除外）<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//下面两句是等同的</span></span><br><span class="line">person1.setName <span class="string">'kk'</span></span><br><span class="line">person1.setName(<span class="string">'kk'</span>)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span>&#123;</span><br><span class="line">	<span class="function">def name</span><br><span class="line">	def age</span><br><span class="line">	String <span class="title">toString</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="string">"$name, $age"</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">def p = <span class="keyword">new</span> Person()</span><br><span class="line">p.name = <span class="string">'kk'</span></span><br><span class="line">p.setAge(<span class="number">22</span>)<span class="comment">//默认存在对应的set， get方法</span></span><br><span class="line">println p <span class="comment">//输出kk, 22，重写了toString方法</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//也可以用groovy的风格做同样的事：</span></span><br><span class="line">def person2=<span class="keyword">new</span> Person([<span class="string">'name'</span>:<span class="string">'gg'</span>,<span class="string">'age'</span>:<span class="number">22</span>]) <span class="comment">//[]号可以省略</span></span><br><span class="line">println person2 <span class="comment">//输出 gg, 22</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<h2 id="u5355_u5143_u6D4B_u8BD5"><a href="#u5355_u5143_u6D4B_u8BD5" class="headerlink" title="单元测试"></a>单元测试</h2><p>　　首先添加junit的jar包，再编写测试用例：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.junit.*;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestPerson</span> </span>&#123;</span><br><span class="line">       <span class="annotation">@Test</span></span><br><span class="line">       <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testToString</span><span class="params">()</span></span>&#123;</span><br><span class="line">              Person p=<span class="keyword">new</span> Person();              <span class="comment">//注意因为groovy编译Person时默认所有属性为private</span></span><br><span class="line">              p.setName(<span class="string">"ddd"</span>);                <span class="comment">//所以用set方法设置name属性而不用p.name=”ddd”</span></span><br><span class="line">              p.setAge(<span class="number">18</span>);</span><br><span class="line">              Assert.assertEquals(<span class="string">"ddd-18"</span>, p.toString());</span><br><span class="line">       &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　下面使用groovy书写测试用例<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.junit.*;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">GroovyTestPerson</span> </span>&#123;</span><br><span class="line">    <span class="annotation">@Test</span></span><br><span class="line">     <span class="function"><span class="keyword">void</span> <span class="title">testToString</span><span class="params">()</span></span>&#123;</span><br><span class="line">       Person p=<span class="keyword">new</span> Person(<span class="string">"name"</span>:<span class="string">"ddd"</span>,<span class="string">"age"</span>:<span class="number">18</span>)</span><br><span class="line">       Assert.assertEquals(<span class="string">"ddd-18"</span>, p.toString())</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　可以看到，这里使用的完全是Groovy风格的书写方式：不需要public，使用map参数构造对象。然而当Run As a Junit Test的时候，结果跟用java编写的测试用例没有什么两样。这也充分说明了，groovy和java，除了语法不一样，本质上没有什么区别（对比.net framework中的C#和VB.net，它们除了语法不同外，本质上它们都使用CLR）。</p>
<h2 id="u53EF_u53D8_u53C2_u6570"><a href="#u53EF_u53D8_u53C2_u6570" class="headerlink" title="可变参数"></a>可变参数</h2><p>　　等同于java 5中的变长参数。首先我们定义一个变长参数的方法sum：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">sum</span><span class="params">(<span class="keyword">int</span>... var)</span> </span>&#123;</span><br><span class="line">    def total = <span class="number">0</span></span><br><span class="line">    <span class="keyword">for</span> (i in var)</span><br><span class="line">        total += i</span><br><span class="line">    <span class="keyword">return</span> total</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//调用sum时使用任意个数的参数：</span></span><br><span class="line"><span class="function">println <span class="title">sum</span><span class="params">(<span class="number">1</span>)</span> <span class="comment">//输出1</span></span><br><span class="line">println <span class="title">sum</span><span class="params">(<span class="number">1</span>,<span class="number">2</span>)</span> <span class="comment">//输出3</span></span><br><span class="line">println <span class="title">sum</span><span class="params">(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>)</span> <span class="comment">//输出6</span></span><br><span class="line">println <span class="title">sum</span><span class="params">(<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>)</span> <span class="comment">//输出4</span></span></span><br></pre></td></tr></table></figure></p>
<h2 id="u679A_u4E3E"><a href="#u679A_u4E3E" class="headerlink" title="枚举"></a>枚举</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//定义一个enum：</span></span><br><span class="line"><span class="keyword">enum</span> Day &#123;</span><br><span class="line">    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//在switch语句中使用</span></span><br><span class="line">def today = Day.<span class="function">SATURDAY</span><br><span class="line"><span class="title">switch</span> <span class="params">(today)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">case</span> [Day.SATURDAY, Day.SUNDAY]:</span><br><span class="line">    println <span class="string">"Weekends are cool"</span></span><br><span class="line">    <span class="keyword">break</span></span><br><span class="line">  <span class="keyword">case</span> Day.MONDAY..Day.FRIDAY: <span class="comment">//a day between Monday and Friday</span></span><br><span class="line">    println <span class="string">"Boring work day"</span></span><br><span class="line">    <span class="keyword">break</span></span><br><span class="line">  <span class="keyword">default</span>:</span><br><span class="line">    println <span class="string">"Are you sure this is a valid day?"</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//注意，switch和case中可以使用任何对象，尤其是可以在case中使用List和范围，从而使分支满足多个条件。</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//同java5一样，groovy支持带构造器、属性和方法的enum：</span></span><br><span class="line"><span class="keyword">enum</span> Planet &#123;</span><br><span class="line"> MERCURY(<span class="number">3.303e+23</span>, <span class="number">2.4397e6</span>),</span><br><span class="line"> VENUS(<span class="number">4.869e+24</span>, <span class="number">6.0518e6</span>),</span><br><span class="line"> EARTH(<span class="number">5.976e+24</span>, <span class="number">6.37814e6</span>),</span><br><span class="line"> MARS(<span class="number">6.421e+23</span>, <span class="number">3.3972e6</span>),</span><br><span class="line"> JUPITER(<span class="number">1.9e+27</span>,<span class="number">7.1492e7</span>),</span><br><span class="line"> SATURN(<span class="number">5.688e+26</span>, <span class="number">6.0268e7</span>),</span><br><span class="line"> URANUS(<span class="number">8.686e+25</span>, <span class="number">2.5559e7</span>),</span><br><span class="line"> NEPTUNE(<span class="number">1.024e+26</span>, <span class="number">2.4746e7</span>)</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">double</span> mass</span><br><span class="line"> <span class="keyword">double</span> radius</span><br><span class="line"> </span><br><span class="line"> <span class="title">Planet</span><span class="params">(<span class="keyword">double</span> mass, <span class="keyword">double</span> radius)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">this</span>.mass = mass;</span><br><span class="line">  <span class="keyword">this</span>.radius = radius;</span><br><span class="line"> &#125;</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">printMe</span><span class="params">()</span> </span>&#123;</span><br><span class="line">  println <span class="string">"$&#123;name()&#125; has a mass of $&#123;mass&#125; "</span> + <span class="string">"and a radius of $&#123;radius&#125;"</span></span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br><span class="line">Planet.EARTH.printMe() <span class="comment">//输出EARTH has a mass of 5.976E24 and a radius of 6378140.0</span></span><br><span class="line">println Planet.EARTH.getMass() <span class="comment">//输出5.976E24</span></span><br><span class="line">println Planet.EARTH.mass <span class="comment">//输出5.976E24</span></span><br></pre></td></tr></table></figure>
<h2 id="Elvis_u64CD_u4F5C_u7B26"><a href="#Elvis_u64CD_u4F5C_u7B26" class="headerlink" title="Elvis操作符"></a>Elvis操作符</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//这是三目运算符“?:”的简单形式，三目运算符通常以这种形式出现：</span></span><br><span class="line">String displayName = name != <span class="keyword">null</span> ? name : <span class="string">"Unknown"</span>;</span><br><span class="line"><span class="comment">//在groovy中，也可以简化为（因为null在groovy中可以转化为布尔值false）：</span></span><br><span class="line">String displayName = name ? name : <span class="string">"Unknown"</span>;</span><br><span class="line"><span class="comment">//基于“不重复”的原则，可以使用elvis操作符再次简化为：</span></span><br><span class="line">String displayName = name ?: <span class="string">"Unknown"</span></span><br><span class="line"></span><br><span class="line">def name = <span class="keyword">null</span></span><br><span class="line">String displayName = name ?: <span class="string">"Unknown"</span></span><br><span class="line">println displayName <span class="comment">//输出Unknown</span></span><br></pre></td></tr></table></figure>
<h2 id="u52A8_u6001_u6027"><a href="#u52A8_u6001_u6027" class="headerlink" title="动态性"></a>动态性</h2><p>　　Groovy所有的对象都有一个元类metaClass，我们可以通过metaClass属性访问该元类。通过元类，可以为这个对象增加方法（在java中不可想象）！见下面的代码，msg是一个String，通过元类，我们为msg增加了一个String 类中所没有的方法up：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">def msg = <span class="string">"Hello!"</span></span><br><span class="line"><span class="comment">//输出org.codehaus.groovy.runtime.HandleMetaClass@65869953[groovy.lang.MetaClassImpl@65869953[class java.lang.String]]</span></span><br><span class="line">println msg.metaClass</span><br><span class="line"></span><br><span class="line">String.metaClass.up = &#123; delegate.toUpperCase() &#125; <span class="comment">//类比javascript</span></span><br><span class="line"><span class="comment">//输出HELLO</span></span><br><span class="line">println msg.up()</span><br><span class="line"></span><br><span class="line"><span class="comment">//通过元类还可以检索对象所拥有的方法和属性（就象反射）：</span></span><br><span class="line"><span class="comment">//methods返回List&lt;MetaMethod&gt;</span></span><br><span class="line">msg.metaClass.methods.each &#123; println it.name &#125; </span><br><span class="line"></span><br><span class="line"><span class="comment">/*输出</span><br><span class="line">up</span><br><span class="line">......</span><br><span class="line">bytes</span><br><span class="line">empty</span><br><span class="line">*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//properties返回List&lt;MetaProperty&gt;</span></span><br><span class="line">msg.metaClass.properties.each &#123; println it.name &#125;</span><br><span class="line"><span class="comment">/*输出</span><br><span class="line">class</span><br><span class="line">bytes</span><br><span class="line">empty*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//甚至可以看到刚才添加的up方法,可以通过元类判断有没有一个叫up的方法，然后再调用它：</span></span><br><span class="line"><span class="keyword">if</span> (msg.metaClass.respondsTo(msg, <span class="string">'up'</span>)) &#123;</span><br><span class="line">    println msg.toUpperCase()</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//当然，也可以推断它有没有一个叫bytes的属性：</span></span><br><span class="line"><span class="keyword">if</span> (msg.metaClass.hasProperty(msg, <span class="string">'bytes'</span>)) &#123;</span><br><span class="line">    println msg.bytes.encodeBase64()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="swing"><a href="#swing" class="headerlink" title="swing"></a>swing</h2><p>　　到现在为止，groovy一直都在控制台窗口下工作。如果你还不满足，当然也可以使用swingbuilder来构建程序：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> groovy.swing.SwingBuilder</span><br><span class="line"><span class="keyword">import</span> java.awt.BorderLayout</span><br><span class="line"><span class="keyword">import</span> groovy.swing.SwingBuilder</span><br><span class="line"><span class="keyword">import</span> java.awt.BorderLayout as BL</span><br><span class="line">def swing = <span class="keyword">new</span> SwingBuilder()</span><br><span class="line">count = <span class="number">0</span></span><br><span class="line">def textlabel</span><br><span class="line">def frame = swing.frame(title:<span class="string">'Frame'</span>, size:[<span class="number">400</span>,<span class="number">400</span>]) &#123;</span><br><span class="line"> borderLayout()</span><br><span class="line"> textlabel = label(</span><br><span class="line">  text:<span class="string">"Clicked $&#123;count&#125; time(s)."</span>,</span><br><span class="line">  constraints: BL.NORTH</span><br><span class="line"> )</span><br><span class="line"> button(</span><br><span class="line">  text:<span class="string">'Click Me'</span>,</span><br><span class="line">  actionPerformed: &#123;</span><br><span class="line">   count++;</span><br><span class="line">   textlabel.text = <span class="string">"Clicked $&#123;count&#125; time(s)."</span>;</span><br><span class="line">   println <span class="string">"clicked"</span></span><br><span class="line">  &#125;,</span><br><span class="line">  constraints:BorderLayout.SOUTH</span><br><span class="line"> )</span><br><span class="line">&#125;</span><br><span class="line">frame.pack()</span><br><span class="line">frame.setVisible(<span class="keyword">true</span>)</span><br></pre></td></tr></table></figure></p>
<h2 id="JVM_u4E2D_u8FD0_u884Cgroovy_u7C7B_u7684_u4E24_u79CD_u65B9_u5F0F"><a href="#JVM_u4E2D_u8FD0_u884Cgroovy_u7C7B_u7684_u4E24_u79CD_u65B9_u5F0F" class="headerlink" title="JVM中运行groovy类的两种方式"></a>JVM中运行groovy类的两种方式</h2><p>　　1. 使用groovyc编译所有的<em>.groovy为java的</em>.class文件，把这些<em>.class文件放在java类路径中，通过java类加载器来加载这些类。<br>　　2. 通过groovy的类加载器在运行时直接加载</em>.groovy文件并且生成对象，在这种方式下，没有生成任何<em>.class，但是生成了一个java.lang.Class对象的实例，也就是说，当groovy代码中包括一个new MyClass()的表达式时，并且也有一个MyClass.groovy的文件，这个文件将被解释，一个MyClass的类型将被产生并且增加到类加载器中，在代码中将像从</em>.class一样获取到MyClass对象。<br>　　图2.7介绍了转换*.groovy文件为java类的两种方法，任何一种途径产生的类都有相同的java类格式，groovy在源代码级增强java，但是在字节码是与java一样的。<a href="/images/groovy-runtime.png">groovy-runtime.png</a></p>
<h2 id="u53C2_u8003_u8D44_u6599"><a href="#u53C2_u8003_u8D44_u6599" class="headerlink" title="参考资料"></a>参考资料</h2><p><a href="http://ldlnew.blog.163.com/blog/static/10607462010542162399/" target="_blank" rel="external">http://ldlnew.blog.163.com/blog/static/10607462010542162399/</a><br><a href="http://syc001.iteye.com/blog/1022144" target="_blank" rel="external">http://syc001.iteye.com/blog/1022144</a><br><a href="http://developer.51cto.com/art/200710/58087.htm" target="_blank" rel="external">http://developer.51cto.com/art/200710/58087.htm</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2015/12/01/groovy/1.groovy入门/" data-id="cijzzpvf300ef1cfsp8p5ogvi" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/groovy/">groovy</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2015/12/01/linux/1.Linux系列之常用命令/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Linux系列一之常用命令
        
      </div>
    </a>
  
  
    <a href="/2015/12/01/rpc/1.RPC系列之入门/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">RPC系列一之入门</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/cache/">cache</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/database/">database</a><span class="category-list-count">13</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/docker/">docker</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/gis/">gis</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/git/">git</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/go/">go</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/gradle/">gradle</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/groovy/">groovy</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/hadoop/">hadoop</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/java/">java</a><span class="category-list-count">18</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/linux/">linux</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/log/">log</a><span class="category-list-count">11</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/mongodb/">mongodb</a><span class="category-list-count">14</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/mq/">mq</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/mybatis/">mybatis</a><span class="category-list-count">9</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/netty/">netty</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/nginx/">nginx</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/nio/">nio</a><span class="category-list-count">19</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/nodejs/">nodejs</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/redis/">redis</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/rpc/">rpc</a><span class="category-list-count">14</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/spring/">spring</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/tomcat/">tomcat</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/zookeeper/">zookeeper</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/杂项/">杂项</a><span class="category-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/aop/">aop</a><span class="tag-list-count">8</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/aspectj/">aspectj</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/cache/">cache</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/classloader/">classloader</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/common-loggings/">common-loggings</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/commonjs/">commonjs</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/database/">database</a><span class="tag-list-count">10</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/docker/">docker</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/dubbo/">dubbo</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/fastdfs/">fastdfs</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gclib/">gclib</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/geohash/">geohash</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gis/">gis</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git/">git</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/go/">go</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gradle/">gradle</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/groovy/">groovy</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hadoop/">hadoop</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hessisan/">hessisan</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/">java</a><span class="tag-list-count">48</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/javassist/">javassist</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/kafka/">kafka</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/">linux</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/log/">log</a><span class="tag-list-count">11</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/log4j/">log4j</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/logback/">logback</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/map/">map</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/memecached/">memecached</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mina/">mina</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mongodb/">mongodb</a><span class="tag-list-count">14</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mq/">mq</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mqtt/">mqtt</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mybatis/">mybatis</a><span class="tag-list-count">9</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/netty/">netty</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nginx/">nginx</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nio/">nio</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nodejs/">nodejs</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/postgresql/">postgresql</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/protobuf/">protobuf</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/proxy/">proxy</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/push/">push</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/rabbitmq/">rabbitmq</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/redis/">redis</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/rpc/">rpc</a><span class="tag-list-count">14</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/sitemesh/">sitemesh</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/sld/">sld</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/slf4j/">slf4j</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/socket/">socket</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spatialite-for-android/">spatialite for android</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spi/">spi</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spring/">spring</a><span class="tag-list-count">13</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/tomcat/">tomcat</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/transaction/">transaction</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/web/">web</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/xSocket/">xSocket</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/zbus/">zbus</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/zookeeper/">zookeeper</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/数据结构/">数据结构</a><span class="tag-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/aop/" style="font-size: 15.38px;">aop</a> <a href="/tags/aspectj/" style="font-size: 11.54px;">aspectj</a> <a href="/tags/cache/" style="font-size: 11.54px;">cache</a> <a href="/tags/classloader/" style="font-size: 10px;">classloader</a> <a href="/tags/common-loggings/" style="font-size: 10px;">common-loggings</a> <a href="/tags/commonjs/" style="font-size: 12.31px;">commonjs</a> <a href="/tags/database/" style="font-size: 16.92px;">database</a> <a href="/tags/docker/" style="font-size: 13.85px;">docker</a> <a href="/tags/dubbo/" style="font-size: 10px;">dubbo</a> <a href="/tags/fastdfs/" style="font-size: 10px;">fastdfs</a> <a href="/tags/gclib/" style="font-size: 10.77px;">gclib</a> <a href="/tags/geohash/" style="font-size: 10px;">geohash</a> <a href="/tags/gis/" style="font-size: 10px;">gis</a> <a href="/tags/git/" style="font-size: 10.77px;">git</a> <a href="/tags/go/" style="font-size: 10px;">go</a> <a href="/tags/gradle/" style="font-size: 10px;">gradle</a> <a href="/tags/groovy/" style="font-size: 10px;">groovy</a> <a href="/tags/hadoop/" style="font-size: 10px;">hadoop</a> <a href="/tags/hessisan/" style="font-size: 11.54px;">hessisan</a> <a href="/tags/java/" style="font-size: 20px;">java</a> <a href="/tags/javassist/" style="font-size: 10px;">javassist</a> <a href="/tags/kafka/" style="font-size: 11.54px;">kafka</a> <a href="/tags/linux/" style="font-size: 12.31px;">linux</a> <a href="/tags/log/" style="font-size: 17.69px;">log</a> <a href="/tags/log4j/" style="font-size: 12.31px;">log4j</a> <a href="/tags/logback/" style="font-size: 13.08px;">logback</a> <a href="/tags/map/" style="font-size: 10px;">map</a> <a href="/tags/memecached/" style="font-size: 10.77px;">memecached</a> <a href="/tags/mina/" style="font-size: 13.08px;">mina</a> <a href="/tags/mongodb/" style="font-size: 19.23px;">mongodb</a> <a href="/tags/mq/" style="font-size: 13.85px;">mq</a> <a href="/tags/mqtt/" style="font-size: 10.77px;">mqtt</a> <a href="/tags/mybatis/" style="font-size: 16.15px;">mybatis</a> <a href="/tags/netty/" style="font-size: 10px;">netty</a> <a href="/tags/nginx/" style="font-size: 11.54px;">nginx</a> <a href="/tags/nio/" style="font-size: 13.08px;">nio</a> <a href="/tags/nodejs/" style="font-size: 10px;">nodejs</a> <a href="/tags/postgresql/" style="font-size: 10.77px;">postgresql</a> <a href="/tags/protobuf/" style="font-size: 10px;">protobuf</a> <a href="/tags/proxy/" style="font-size: 10px;">proxy</a> <a href="/tags/push/" style="font-size: 10px;">push</a> <a href="/tags/rabbitmq/" style="font-size: 11.54px;">rabbitmq</a> <a href="/tags/redis/" style="font-size: 13.85px;">redis</a> <a href="/tags/rpc/" style="font-size: 19.23px;">rpc</a> <a href="/tags/sitemesh/" style="font-size: 10px;">sitemesh</a> <a href="/tags/sld/" style="font-size: 10px;">sld</a> <a href="/tags/slf4j/" style="font-size: 10px;">slf4j</a> <a href="/tags/socket/" style="font-size: 14.62px;">socket</a> <a href="/tags/spatialite-for-android/" style="font-size: 10px;">spatialite for android</a> <a href="/tags/spi/" style="font-size: 10px;">spi</a> <a href="/tags/spring/" style="font-size: 18.46px;">spring</a> <a href="/tags/tomcat/" style="font-size: 10.77px;">tomcat</a> <a href="/tags/transaction/" style="font-size: 10px;">transaction</a> <a href="/tags/web/" style="font-size: 10px;">web</a> <a href="/tags/xSocket/" style="font-size: 14.62px;">xSocket</a> <a href="/tags/zbus/" style="font-size: 10px;">zbus</a> <a href="/tags/zookeeper/" style="font-size: 10.77px;">zookeeper</a> <a href="/tags/数据结构/" style="font-size: 10.77px;">数据结构</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/01/">January 2016</a><span class="archive-list-count">67</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/12/">December 2015</a><span class="archive-list-count">81</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/11/">November 2015</a><span class="archive-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recents</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2016/01/30/资料/">(no title)</a>
          </li>
        
          <li>
            <a href="/2016/01/30/java/13.Classloader系列一/">ClassLoader系列一</a>
          </li>
        
          <li>
            <a href="/2016/01/29/hadoop/1.hadoop系列之源码编译/">hadoop系列一之源码编译</a>
          </li>
        
          <li>
            <a href="/2016/01/29/go/1.GO系列之入门/">Go系列一之入门</a>
          </li>
        
          <li>
            <a href="/2016/01/28/tomcat/2.tomcat性能优化/">tomcat性能优化</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2016 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="http://libs.baidu.com/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css" type="text/css">
  <script src="/fancybox/jquery.fancybox.pack.js" type="text/javascript"></script>


<script src="/js/script.js" type="text/javascript"></script>

  </div>
</body>
</html>